package com.tjp.eight.ui.fragment.personsetting;

import android.util.Log;

import com.tjp.eight.AllRetrofit;
import com.tjp.eight.ResultModel;
import com.tjp.eight.base.BasePresenterImpl;
import com.tjp.eight.constant.UrlConstant;
import com.tjp.eight.net.NetCallBack;
import com.tjp.eight.net.SubscribeCallBack;
import com.tjp.eight.utils.AppUtils;
import com.tjp.eight.views.LogoutDailog;

import java.io.File;
import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/3/1.
 */
public class PersonSettingPresenter extends BasePresenterImpl<PersonSettingView> {
    private PersonService service = AllRetrofit.getRetrofit(UrlConstant.BaseUrl).create(PersonService.class);
    private PersonService servicePic = NetWork.getRetrofit(UrlConstant.BaseUrl).create(PersonService.class);

    public PersonSettingPresenter(PersonSettingView view) {
        attachView(view);
    }

    public void loadData(String pJson) {
        RequestBody body = AppUtils.getRequestBody(pJson);
        addSubscription(service.loadData(body), new SubscribeCallBack<>(new NetCallBack<PersonModel>() {
            @Override
            public void onSuccess(PersonModel model) {
                view.loadData(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }

    public void uploadPic(String uid, String loginkey, String imgpath) {
        File file = new File(imgpath);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("uploadpic", file.getName(), requestFile);
        addSubscription(servicePic.uploadpic(uid, loginkey, body), new SubscribeCallBack<>(new NetCallBack<UploadPicRModel>() {
            @Override
            public void onSuccess(UploadPicRModel model) {
                view.uploadSuccess(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }

    public void uploadXingZuo(String xPjson) {
        RequestBody body = AppUtils.getRequestBody(xPjson);
        addSubscription(service.uploadXingZuo(body), new SubscribeCallBack<>(new NetCallBack<ResultModel>() {
            @Override
            public void onSuccess(ResultModel model) {
                view.xingZuoSuccess(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }

    public void modifySex(String sexPJson) {
        RequestBody body = AppUtils.getRequestBody(sexPJson);
        addSubscription(service.modifySex(body), new SubscribeCallBack<>(new NetCallBack<ResultModel>() {
            @Override
            public void onSuccess(ResultModel model) {
                view.sexSuccess(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }

    public void bindingWx(String json) {
        RequestBody body = AppUtils.getRequestBody(json);
        addSubscription(service.bindingwx(body), new SubscribeCallBack<>(new NetCallBack<ResultModel>() {
            @Override
            public void onSuccess(ResultModel model) {
                view.bindingSuccess(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }
}
